<?php

namespace App\Http\Controllers\Home;

use App\Http\Servers\Home\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PageController extends CommonController
{
    public function index(Request $request, BaseServer $baseServer)
    {
        //本地访问不记录访问次数
        if (getIp() != '127.0.0.1') {
            \App\PageInfo::where('id', $request->page)->increment('page_view');
        }

        $tmpPage = \App\PageInfo::where('page_infos.status', 1)
            ->where('page_labels.status', 1)
            ->leftjoin('images', 'images.id', '=', 'page_infos.image')
            ->join('admins', 'admins.id', '=', 'page_infos.author')
            ->join('page_labels', 'page_labels.id', '=', 'page_infos.label')
            ->select('page_infos.*', 'images.path as image', 'admins.name as author', 'page_labels.label as label', 'page_labels.name as label_name');
        if (!empty($request->page)) {
            $data['page'] = $tmpPage->findorfail($request->page)->toArray();
        } else {
            $data['page'] = $tmpPage->first()->toArray();
        }

        if (!empty($data['page']['jump_url'])) {
            return redirect($data['page']['jump_url'], 301);
        }

        if (config('area')) {
            $data['title'] = $data['page']['name'] . '_' . config('set.web_name') . '_' . config('range')[config('area')] . '分站';
        } else {
            $data['title'] = $data['page']['name'] . '_' . config('set.web_name');
        }

        $data['keywords'] = $data['page']['tag'];
        $baseServer->keywordDowork($request, $data['keywords']);

        $data['description'] = !empty($data['page']['simpledescribe']) ? $data['page']['simpledescribe'] : $data['page']['name'];

        $data['frames'] = $baseServer->frame([
            ['url' => route('page', [$data['page']['label'], $request->page]), 'name' => $data['page']['name']]
        ]);

        if ((bool)config('set.wap_jump') && is_mobile()) {
            return view('home.m.' . config('set.wap_theme') . '.page.' . $data['page']['theme'], $data);
        } else {
            return view('home.pc.page.' . $data['page']['theme'], $data);
        }
    }
}
